# -*- coding:utf-8 -*-
"""

"""
import requests
import json
from bs4 import BeautifulSoup
import re
from WriteData import writedata
from Tk import genearteMD5
from cs import day


def get_html_text(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.get(url, timeout=30, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


def get_dates_kx(url):
    text = get_html_text(url)
    if not text:
        return None
    soup = BeautifulSoup(text, 'lxml')
    datelist = soup.select('.kxtime')
    dates = [date.get_text()[:10] for date in datelist]
    return dates


def count_process_one():
    data = dict(d1=0, d2=0, d3=0, d4=0)
    mapping = {
        0: 'd1',
        1: 'd2',
        2: 'd3',
    }
    a = 1
    while a < 50:
        url = 'http://www.jianyicaijing.com/portal.php?mod=list&catid=3&page={}'
        url = url.format(a)
        dates = get_dates_kx(url)
        if not dates:
            continue
        print('----简一财经快讯获取第{}页----'.format(a))
        for date in dates:
            s = day(date)
            mapp = mapping.get(s) or 'd4'
            data[mapp] += 1
        a += 1
        if data['d4'] > 0:
            break
    return (data.get('d1'), data.get('d2'), data.get('d3'))


def main():
    u = '简一财经'
    t1 = count_process_one()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0], "yesterday": t1[1], "frontday": t1[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
